import { AfterViewInit, Component, Injector, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { ConfigModule } from 'src/app/config.module';
import { NavigationEnd, Router } from '@angular/router';
import { Subject } from 'rxjs';
import { filter, takeUntil } from 'rxjs/operators';
import { listAnimation } from '../../../../animations/list.animation';
import { BaseUI } from '../../../../common/base';
import { PostService } from '../service/post.service';
import { TableComponent } from '../../../../components/table/table.component';
import { ColumnType } from '../../../../components/table/enums/ColumnType';
import {
    EventKeyEnum,
    EventPublishDTO,
    OptionTypeEnum,
    TableOptionModel
} from '../../../../components/table/table-option/table-option.model';
import { DateUtil } from '../../../../utils/DateUtil';
import { GlobalConfig } from '../../../../common/GlobalConfig';
import { PostOptionComponent } from '../component/post-option/post-option.component';

@Component({
    selector: 'app-role',
    templateUrl: './index.component.html',
    styleUrls: ['./index.component.scss'],
    animations: [listAnimation]
})
export class PostComponent extends BaseUI implements OnInit, AfterViewInit, OnDestroy {

    @ViewChild('optionComponent') optionComponent: PostOptionComponent;

    onDestroy$: Subject<any> = new Subject<any>();
    constructor(
        public _config: ConfigModule,
        private _router: Router,
        private _injector: Injector,
        public mainService: PostService
    ) {
        super(_injector);

        this._router.events
            .pipe(
                filter((ev) => ev instanceof NavigationEnd),
                takeUntil(this.onDestroy$)
            )
            .subscribe(() => {
            });
    }

    ngOnInit() {
    }

    ngAfterViewInit() {}

    ngOnDestroy() {
        this.onDestroy$.complete();
    }

    /**
     * 表格相关
     */

    @ViewChild("mainTable")
    mainTable: TableComponent;

    searchObj:any = {
        pageNum: 1,
        deptId: '',
        pageSize: this._config.pageSize,
        name: '',
        phonenumber: '',
        emailNumber: '',
        status: "",
    };

    columnArray = [
        {type: ColumnType.TEXT, name: '岗位编号', key: 'postId'},
        {type: ColumnType.TEXT, name: '岗位编码', key: 'postCode'},
        {type: ColumnType.TEXT, name: '岗位名称', key: 'postName'},
        {type: ColumnType.TEXT, name: '岗位排序', key: 'postSort'},
        {type: ColumnType.LABEL, name: '状态', key: 'status'},
        {type: ColumnType.TEXT, name: '创建时间', key: 'createTime'},
    ];
    optionModelList: TableOptionModel[] = [
        {
            optionLabel:"修改",
            optionLabelType: OptionTypeEnum.TEXT,
            eventType:"",
            eventKey:EventKeyEnum.EDIT
        },
        {
            optionLabel:"删除",
            optionLabelType: OptionTypeEnum.TEXT,
            eventType:"",
            eventKey:EventKeyEnum.DELETE
        }
    ]
    otherOptionModelList: TableOptionModel[] = [
        {
            optionLabel:"新增",
            optionLabelType: OptionTypeEnum.BUTTON,
            eventType:"",
            eventKey:EventKeyEnum.ADD,
        },
        {
            optionLabel:"批量删除",
            optionLabelType: OptionTypeEnum.BUTTON,
            eventType:"",
            eventKey:EventKeyEnum.BATCH_DELETE,
        },
        {
            optionLabel:"导出",
            optionLabelType: OptionTypeEnum.BUTTON,
            eventType:"",
            eventKey:EventKeyEnum.EXPORT,
        }
    ]

    globalCaseMap = GlobalConfig.globalCaseMap;

    optionEvent($event: EventPublishDTO) {
        let data  = $event.publishData;
        switch ($event.eventKey) {
            case EventKeyEnum.EDIT:
                this.optionComponent.onModalOpen(data.postId);
                break;
            case EventKeyEnum.ADD:
                this.optionComponent.onModalOpen(null);
                break;
            default:
                this.messageService.warning("等待开发中......")
        }
    }

    searchList() {
        this.mainTable.list();
    }

    keyEnter($event: KeyboardEvent) {
        if($event.key === 'Enter'){
            this.searchList();
        }
    }


    /**
     * 日期选择
     */

    datePick: string[]=[];
    onDatePick() {
        this.searchObj.params = {};
        this.searchObj.params['beginTime'] = DateUtil.formatDate(this.datePick[0],"yyyy-MM-dd");
        this.searchObj.params['endTime'] = DateUtil.formatDate(this.datePick[1],"yyyy-MM-dd");
    }

    resetSearchObject() {
        this.searchObj = {
            pageNum: 1,
            pageSize: this._config.pageSize,
        };
    }
}
